//
//  TestPOPCtr.m
//  iOSDemo
//
//  Created by mokai on 14-10-10.
//  Copyright (c) 2014年 mokai. All rights reserved.
//

#import "TestPOPCtr.h"
#import <pop/POP.h> //1、导入pop.h
#import "AnimationsListViewController.h"

@interface TestPOPCtr ()
@property (weak, nonatomic) IBOutlet UIView *image;

@end

@implementation TestPOPCtr

- (IBAction)resetImage:(UIView *)sender {
    [UIView beginAnimations:@"" context:@""];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    sender.frame = CGRectMake(38, 83, 255, 132);
    [UIView commitAnimations];
}

/***
 1、Spring弹簧动画
 formValue toValue 从哪到哪
 springBounciness 反弹力度
 springSpeed 反弹速度
 ***/
- (IBAction)testSpringAnim:(UIButton *)sender {
    
    POPSpringAnimation *springAnim =  [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition];//指定位置做出动画效果
    springAnim.toValue = [NSValue valueWithCGPoint:self.view.center];//从当前位置到目标位置
    springAnim.springBounciness = 20;//反弹效果
    springAnim.springSpeed = 20;//速度
    
    [_image.layer pop_addAnimation:springAnim forKey:@"moveToCenter"];
}

/**
 2、Decay衰慢动画
 ***/
- (IBAction)testDecayAnim:(id)sender {
    POPDecayAnimation *anim = [POPDecayAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    anim.velocity = @(100); //移动100毫米即停止
    [_image pop_addAnimation:anim forKey:@"slide"];
}

- (IBAction)POPBasicAnimation:(id)sender {
    POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    anim.fromValue = @(0.0);
    anim.toValue = @(1.0);
    [_image pop_addAnimation:anim forKey:@"fade"];
}

/***
 关于Property
 主要针对某项属性触发动画
 如，针对view大小 -> kPOPViewSize
 针对view位置 -> kPOPLayerPosition
 
 
 也可以自定义Property
 **/

- (IBAction)customerProperty:(id)sender {
    POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"my_positions_x" initializer:^(POPMutableAnimatableProperty *prop) {
        // read value
        prop.readBlock = ^(CALayer *obj, CGFloat values[]) {
            values[0] = [(CALayer *)obj position].x;

        };
        // write value
        prop.writeBlock = ^(CALayer *obj, const CGFloat values[]) {
            CGPoint p = [(CALayer *)obj position];
            p.x = values[0];
            [obj setPosition:p];
        };
    }];
    POPSpringAnimation *pop = [POPSpringAnimation animation];
    pop.toValue = @(20.);
    pop.property = prop;//指定Property
    
    [_image.layer pop_addAnimation:pop forKey:@"customerProperty"];
}

/***
 poping开源库，基于pop的各种效果
 
 ***/
- (IBAction)testPoping:(id)sender {
    
    AnimationsListViewController *animationsListViewController = [AnimationsListViewController new];
    
    [self.navigationController pushViewController:animationsListViewController animated:YES];
    
}

@end
